package compiler.terminals;

import howl.types.Unit;

import java.net.URL;

import compiler.HowlToken;
import compiler.HowlType;
import compiler.TokenReader;
import compiler.exceptions.HowlParserException;

/**
 * A data declaration consists of a name and a dimension. This class is used
 * when creating new feeds.
 * 
 * TODO: Split into two subclasses, one for URLs one for Units?
 * 
 * @author Sarah Mount <s.mount@wlv.ac.uk>
 * @version 16 Dec 2009
 * 
 * @see compiler.nonterminals.CreateStmt
 * @see compiler.terminals.Unit
 * @see java.net.URL
 */
public class DataDecl extends HowlToken {

	private String name;
	private Unit dimension;
	private URL url;

	public DataDecl(TokenReader tokenReader) {
		super(tokenReader);
	}

	@Override
	public String codeGenerate() {
		// TODO: Use units as per GQL.
		return this.name +  " " + dimension.toString();
	}

	@Override
	public HowlToken parse() throws HowlParserException {
		this.name = this.tokenReader.consumeToken();
		String token = this.tokenReader.consumeToken();
		try {
			this.dimension = Unit.valueof(token);
			// TODO: FIXME: Unit.valueof should never return null
			// Instead it should throw a HowlParserException.
			if (this.dimension != null)
				return this;
			else
				throw new HowlParserException(this.tokenReader.getStatement(),
						this.tokenReader.getCharNo());
		} catch (Exception e1) {
			try {
				this.url = new URL(token);
				return this;
			} catch (Exception e2) {
				throw new HowlParserException(this.tokenReader.getStatement(),
						this.tokenReader.getCharNo());
			}
		}
	}

	@Override
	public String toString() {
		if (!(this.url == null))
			return this.name + " " + this.url.toString();
		else
			return this.name + " " + this.dimension.toString();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the dimension
	 */
	public Unit getDimension() {
		return dimension;
	}

	/**
	 * @param dimension the dimension to set
	 */
	public void setDimension(Unit dimension) {
		this.dimension = dimension;
	}

	/**
	 * @return the url
	 */
	public URL getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl(URL url) {
		this.url = url;
	}

}
